<template>
    <ynet-content id="requestPin" :headerInformation="true" :headerMenu="true" class="main-container">
        <div ><!--提示当前位置  电话银行账户状态维护-->
			<ynet-breadcrumb></ynet-breadcrumb>
		</div>
        <div  v-if="main">
            <div>
                <el-form ref="queryForm" :model="queryForm" class="formEmbedTable mt20" :rules="rules">
                    <table>
                        <caption>Enquiry</caption>
                        <tr>
                            <td> TBS A/C No.</td>
                            <td colspan='2'>
                                <el-form-item prop="accNo">
                                    <el-input v-model="queryForm.accNo"></el-input>
                                </el-form-item>
                            </td>					
                        </tr>
                    </table>
                </el-form>
            </div>
            <div class="btnArea mt20 tac">
                <el-button type="primary" @click="queryAccInfo">Search</el-button>
            </div>
        </div>
        <div>
                <el-form ref="accInfoDate" :model="accInfoDate" class="formEmbedTable mt20">
                    <table>
                        <caption>Operation</caption>
                        <tr>
                            <td>Primary A/C No.</td>
                            <td>
                                <el-form-item  prop="accNo">
                                    <el-input v-model="accInfoDate.accNo" :disabled="true"></el-input>
                                </el-form-item>              
                            </td>
                        </tr>	
                        <tr>
                            <td>Customer Name</td>
                            <td>
                                <el-form-item  prop="accName">
                                    <el-input v-model="accInfoDate.accName" :disabled="true"></el-input>
                                </el-form-item>             
                            </td>
                        </tr> 
                        <tr>
                            <td>TBS A/C Status</td>
                            <td>
                                <el-form-item  prop="accStatus">
                                    <el-select v-model="accInfoDate.accStatus" :disabled="true">
                                        <template v-for="item in actionOptions">
                                            <el-option :label="item.label" :value="item.value"></el-option>
                                        </template>         
                                    </el-select>
                                </el-form-item>             
                            </td>
                        </tr>
               <!--          <tr>
                            <td>TBS A/C Last Active Date</td>
                            <td>
                                <el-form-item  prop="activeDate">
                                    <el-input v-model="accInfoDate.activeDate" :disabled="true"></el-input>
                                </el-form-item>             
                            </td>
                        </tr>  -->
                    </table>
                </el-form>
                <el-form ref="changeStatusForm" :model="changeStatusForm" class="formEmbedTable mt20" :rules="rules1">
                    <table>
                        <tr>
                            <td>Action</td>
                            <td>
                                <el-form-item  prop="action">
                                    <el-select v-model="changeStatusForm.action">
                                        <template v-for="item in actionOptions">
                                            <el-option :label="item.label" :value="item.value"></el-option>
                                        </template>         
                                    </el-select>
                                </el-form-item>             
                            </td>
                        </tr>

                        <tr>
                            <td>CHANGE_STATUS_REASON</td>
                            <td>
                                <el-form-item  prop="reason">
                                    <el-input v-model="changeStatusForm.reason"></el-input>
                                </el-form-item>             
                            </td>
                        </tr>
                        	
                    </table>
                </el-form>
                <div class="btnArea mt20 tac">
                    <el-button type="primary" @click="queryAuth">Confime</el-button>
                </div>
        </div>
        
        <!--授权确认页面弹窗-->
        <div>
            <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
        </div>
    </ynet-content>
</template>

<script>
	export default {
		name:'accStateOper',
		components:{
		},
		data(){
            return { 
                main:true,
                actionOptions:[
                    {value:'0',label:'activation'},
                    {value:'1',label:'freeze'},
                    {value:'2',label:'Cancellation'},
                ],
                rules:{
                    accNo:[
                        {validator:this.validatorAcc, trigger:'blur'},
                    ],
                },
                rules1:{
                    action:[
                        {required:true,message:'Can not be enpty',trigger:'blur'},
                    ],
                    reason:[
                        {required:true,message:'Can not be enpty',trigger:'blur'},
                    ],
                },
                queryForm:{
                    accNo:'',
                },
                accInfoDate:{
                    accNo:'',
                    // accName:'',
                    accStatus:'',
                    // activeDate:'',
                },
                changeStatusForm:{
                    action:'',
                    reason:'',
                },
                confimButton:false,
                needAuth:'',
                authType:'',
                authForm: { authUserNo: '', password: '',randomField:''},
            }   
        },
		methods:{ 
            
            // resetForm(formName){
            //         this.$refs[formName].resetFields();
            // },
            //账号校验
           validatorAcc(rule, value, callback){
                let _this = this
                if("" == value || undefined == value) return callback("Please input account number")
                if(!(/^[0-9]*$/.test(value))){
                    return callback("Please enter the correct format of the account !") 
                }
                callback()
            },
            getAuthForm(val){
                //获取授权数据
                this.authForm.authUserNo = val.authUserNo
                this.authForm.password = val.password
                this.authForm.randomField = val.randomField
                this.changeStatus()
            },
            queryAuth(){//查询授权模式
                let body={};body.bsnCode="IM040202";body.version="IM040202",
                this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                    if(data.body.errorCode==0){
                        if(data.body.needAuth==0){//无需授权
                            this.needAuth=0;
                            this.changeStatus()
                            
                        }else if(data.body.needAuth==1 && data.body.authType==1){  // 临桂式授权
                            this.needAuth=1;
                            this.authType=1;
                            this.$refs["dialogTable"].show()
                        }else if( data.body.needAuth==1 && data.body.authType==2 ){   //一录一审
                            this.needAuth=1;
                            this.authType=2;
                            this.changeStatus()
                        }
                    }else{
                        this.$alert("System Error",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })   
			},
            queryAccInfo(){
                this.$refs.queryForm.validate((valid)  => {
                  if(valid){
                        let queryData = {}
                        queryData.reqTP = 'A'
                        queryData.primaryAcc = this.queryForm.accNo
                        this.$tools.request(this,"IM080201Action.do",queryData).then(data=>{
                            console.log("data:"+JSON.stringify(data))
                            if(data.body.errorCode==0){
                                    if('' == data.body.accNo){   //数据库没有记录
                                        this.confimButton = false
                                        this.$alert('No account information, please re-enter !',{
                                            confirmButtonText: 'Confirm',
                                            callback: action => {
                                            }
                                        })
                                    }else {
                                        this.confimButton = true
                                        this.accInfoDate.accNo = data.body.accNo 
                                        this.accInfoDate.accName = data.body.accChName
                                        this.accInfoDate.accStatus = data.body.status 
                                        this.queryForm.accNo = ''
                                    }
                                                    
                            }else{
                                this.confimButton = false
                                this.$alert("System Error Try again", "warning",{
                                        confirmButtonText: 'Confirm',
                                        callback: action => {
                                        }
                                    })
                            }
                        })
                  }
                })
                
            },
            changeStatus(){
                let requestData = {}
                if('' == this.accInfoDate.accNo){
                    return
                }
                requestData.queryAcc = this.accInfoDate.accNo
                this.$tools.request(this,"IM080211Action.do",requestData).then(data=>{ //判断此账号是否在审核中
                    if(data.body.errorCode == '0'){
                        if('1' == data.body.responseFlag){   //待审核
                            this.$alert('This account is subject to authorization information and cannot be operated !',{
                                confirmButtonText: 'Confirm',
                                callback: action => {
                                }
                            })
                        }else if('0' == data.body.responseFlag){
                            this.$refs.changeStatusForm.validate((valid)  => {
                                if(valid){
                                    let queryData = {}
                                    if('2' ==this.accInfoDate.accStatus){  //销户状态不容许维护
                                        this.$alert('The status of the cancellation does not allow maintenance changes',{
                                            confirmButtonText: 'Confirm',
                                            callback: action => {
                                            }
                                        })
                                    }else if(this.changeStatusForm.action == this.accInfoDate.accStatus){
                                        this.$alert('The account status has not changed and no change is required !',{
                                            confirmButtonText: 'Confirm',
                                            callback: action => {
                                            }
                                        })
                                    }else if(('0' == this.accInfoDate.accStatus || '1' == this.accInfoDate.accStatus) && this.confimButton)  {
                                            if(
                                                ('0' == this.accInfoDate.accStatus && '1' == this.changeStatusForm.action) ||
                                                ('1' == this.accInfoDate.accStatus && '0' == this.changeStatusForm.action) ||
                                                ('0' == this.accInfoDate.accStatus && '2' == this.changeStatusForm.action) ||
                                                ('1' == this.accInfoDate.accStatus && '2' == this.changeStatusForm.action)) {
                                                    queryData.primaryAcc = this.accInfoDate.accNo
                                                    queryData.accName = this.accInfoDate.accName
                                                    queryData.accStatus = this.accInfoDate.accStatus
                                                    queryData.action = this.changeStatusForm.action
                                                    queryData.reason = this.changeStatusForm.reason
                                                    queryData.actionType = '1'
                                                    queryData.bsnCode = 'IM040202'
                                                    if(this.needAuth==1 && this.authType==1){
                                                        queryData=$.extend(queryData,this.authForm)
                                                    }
                                                    
                                                    this.$refs.changeStatusForm.validate((valid)  => {
                                                        if(valid){
                                                            this.$tools.request(this,"IM080205Action.do",queryData).then(data=>{
                                                                if(data.body.errorCode==0){
                                                                    if(this.needAuth==1 && this.authType==2){
                                                                        this.$message({
                                                                            type:'success',
                                                                            message:'Waiting for Authorized'
                                                                        })
                                                                        
                                                                    } else if(this.needAuth==1 && this.authType==1){
                                                                        if(data.body.authStatus==1){
                                                                            this.$message({
                                                                                type:'warning',
                                                                                message:'Authorship No. or password wrong ,please resume load',
                                                                                showClose:true,
                                                                            })
                                                                        
                                                                        }else{
                                                                            this.$message({
                                                                                type:'success',
                                                                                message:'Modify Success',
                                                                                showClose:true,
                                                                            })
                                                                        }
                                                                    }else{
                                                                        this.$message({
                                                                            type:'success',
                                                                            message:'Modify Success',
                                                                            showClose:true,
                                                                        })
                                                                    }
                                                                } else{
                                                                    this.$alert("System Error Try again", "warning",{
                                                                            confirmButtonText: 'Confirm',
                                                                            callback: action => {
                                                                            }
                                                                        })
                                                                }
                                                            })
                                                        }
                                                    })
                                                    
                                                }
                                        
                                    }else if(this.confimButton) {
                                        this.$alert('Current account status does not allow this modification !',{
                                            confirmButtonText: 'Confirm',
                                            callback: action => {
                                            }
                                        })
                                    }
                                    
                                }
                            })

                        }
                    }else{
                        this.$alert("System Error Try again", "warning",{
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        })
                    }
                })

                
            },
            
		}
	}
</script>